package com.conan.springjdbc.controller;

import com.conan.springjdbc.bean.Student;
import com.conan.springjdbc.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.Connection;

@RestController
public class JDBCController {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private IStudentService studentService;


    @PostConstruct
    public void  JDBCConnect(){
        Connection connection = null;
        try {
            connection =  dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(dataSource.getClass());
        System.out.println(connection);
    }

    @GetMapping("/insert")
    public void insert(){
        Student student = new Student();
        student.setAge(23);
        student.setId(1003);
        student.setName("jack");
        studentService.insert(student);
    }

    @GetMapping("/update")
    public String  update(){
        Student student = new Student();
        student.setAge(24);
        student.setId(1003);
        student.setName("jack");
        return  studentService.update(student);
    }


}
